/*
  get方式请求流式数据
*/
type Params = {
	url: string
	onMessage: (msg: object | null, evtSource: EventSource) => void
	onEnd: () => void
}

export default async ({ url, onMessage, onEnd }: Params) => {
	const evtSource = new EventSource(url)
	evtSource.onmessage = function (event) {
		try {
			const data = JSON.parse(event.data)
			onMessage(data, evtSource)
		} catch (error) {
			console.log(error, 'err', evtSource)
		}
	}

	evtSource.onerror = function (error) {
		onEnd()
		evtSource.close()
	}
}
